package com.soft.dao;

import com.soft.entity.TrolleyInfo;
import com.soft.util.Connect;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kongz
 * @version 1.0 2021/6/2 16:34
 * @description 购物车信息接口
 */
public class TrolleyInfoDao {

    Class<TrolleyInfo> trolleyInfoClass;

    public TrolleyInfoDao() {
        trolleyInfoClass = TrolleyInfo.class;
    }

    /**
     * @author kongz 2021/6/2 16:35
     * @description 根据trolley获取购物车信息列表
     * @param trolleyId 购物车编号
     * @return TrolleyInfo列表
     */
    public List<TrolleyInfo> getByTrolley(int trolleyId) {
        List<Object> param = new ArrayList<>();
        param.add(trolleyId);

        String sql = "select * from trolleyinfo where trolleyid=?";

        return Connect.getInstance().queryListObject(sql, param, trolleyInfoClass);
    }

    /**
     * @author kongz 2021/6/3 17:02
     * @description 根据id获取购物车信息
     * @param id 购物车信息编号
     * @return TrolleyInfo
     */
    public TrolleyInfo get(int id) {
        List<Object> param = new ArrayList<>();
        param.add(id);

        String sql ="select * from trolleyinfo where id=?";

        return Connect.getInstance().queryObject(sql, param, trolleyInfoClass);
    }

    /**
     * @author kongz 2021/6/3 16:50
     * @description 增加新的购物车信息
     * @param trolleyInfo 购物车信息
     * @return boolean
     */
    public boolean insert(TrolleyInfo trolleyInfo) {
        List<Object> param = new ArrayList<>();
        param.add(trolleyInfo.getTrolleyId());
        param.add(trolleyInfo.getGoodsId());
        param.add(trolleyInfo.getGoodsName());
        param.add(trolleyInfo.getUnitPrice());
        param.add(trolleyInfo.getCount());
        param.add(trolleyInfo.getPrice());

        String sql = "insert into trolleyinfo (trolleyid, goodsid, goodsname, " +
                "unitprice, count, price) VALUE (?,?,?,?,?,?)";

        if (Connect.getInstance().execute(sql, param) != 0)
            return true;
        return false;
    }

    /**
     * @author kongz 2021/6/3 17:06
     * @description 更新购物车信息
     * @param trolleyInfo 购物车信息
     * @return boolean
     */
    public boolean update(TrolleyInfo trolleyInfo) {
        List<Object> param = new ArrayList<>();
        param.add(trolleyInfo.getTrolleyId());
        param.add(trolleyInfo.getGoodsId());
        param.add(trolleyInfo.getGoodsName());
        param.add(trolleyInfo.getUnitPrice());
        param.add(trolleyInfo.getCount());
        param.add(trolleyInfo.getPrice());
        param.add(trolleyInfo.getId());

        String sql = "update trolleyinfo set trolleyid=?,goodsid=?,goodsname=?," +
                "unitprice=?,count=?,price=? where id=?";

        if (Connect.getInstance().execute(sql, param) != 0)
            return true;
        return false;
    }

    /**
     * @author kongz 2021/6/3 17:13
     * @description 删除购物车信息
     * @param id 购物车信息编号
     * @return boolean
     */
    public boolean delete(int id) {
        List<Object> param = new ArrayList<>();
        param.add(id);

        String sql = "delete from trolleyinfo where id=?";

        if (Connect.getInstance().execute(sql, param) != 0)
            return true;
        return false;
    }
}

